/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author VietLH
 */
public class ManLabConnection {

    private String server = "jdbc:sqlserver://localhost:1433;";
    private String databaseName = "databaseName=ManLab";
    private String userName = "sa";
    private String password = "123456";
    private String connectionString = server + databaseName;
    private Connection connetion;
    private ResultSet rs;
    private Statement statement;
    private PreparedStatement pStatement;

    public ManLabConnection() {
    }

    public boolean createConnection() {
        try {
            connetion = DriverManager.getConnection(connectionString, userName, password);
            return true;
        } catch (SQLException ex) {
//            Logger.getLogger(ManLabConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public ResultSet createResultSet(String query) {
        try {
            statement = connetion.createStatement();
            rs = statement.executeQuery(query);
        } catch (SQLException ex) {
            Logger.getLogger(ManLabConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public boolean executeQuery(String query) {
        try {
            pStatement = connetion.prepareStatement(query);
            pStatement.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(ManLabConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public void closeConnection() {
        try {
            connetion.close();
        } catch (SQLException ex) {
            Logger.getLogger(ManLabConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
